package com.token.utils;

import cn.hutool.core.date.LocalDateTimeUtil;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @auther: henriy
 * @Date: 2023/10/31
 */
public class TimeUtils {

    /**
     * 比较两个时间的前后关系
     *
     * @param formerTime 前一个时间（yyyy-MM-dd、yyyy-MM）
     * @param latterTime 后一个时间（yyyy-MM-dd、yyyy-MM）
     * @return 布尔值
     */
    public static boolean compareTwoTimeAround(String formerTime, String latterTime) {

        boolean flag = false;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date formerDate = sdf.parse(formerTime);
            Date latterDate = sdf.parse(latterTime);

            if (latterDate.after(formerDate)) {
                flag = true;
            }
        } catch (ParseException e) {
            System.out.println("时间解析异常");
        }

        return flag;
    }

    /***
     * @param strDate yyyyMMdd类型字符串
     * @return yyyy-MM-dd类型字符串
     */
    public static String toFormatDate(String strDate) {
        String resultDate = "";

        try {
            Date date = new SimpleDateFormat("yyyyMMdd").parse(strDate);
            resultDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }

        return resultDate;
    }

    /**
     * 解析字符串时间为LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate parseDate(String date) {
        return LocalDateTimeUtil.parseDate(date);
    }

    /**
     * 时间转时间戳
     *
     * @param timeStr yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long convertTimeToTimestamp(String timeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = sdf.parse(timeStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        return date.getTime();
    }

    public static long getSecondsDifference(long minTimestamp, long maxTimestamp) {
        Instant minInstant = Instant.ofEpochMilli(minTimestamp);
        Instant maxInstant = Instant.ofEpochMilli(maxTimestamp);
        Duration duration = Duration.between(minInstant, maxInstant);
        return duration.getSeconds();
    }

    public static String convertMillisecondsToTime(long milliseconds) {
        long hours = milliseconds / (60 * 60 * 1000);
        long minutes = (milliseconds % (60 * 60 * 1000)) / (60 * 1000);
        long seconds = (milliseconds % (60 * 1000)) / 1000;

        return String.format("%d小时%d分钟%d秒", hours, minutes, seconds);
    }

    /**
     * 字符串时间转LocalDateTime
     *
     * @param dateStr
     * @return
     */
    public static LocalDateTime strTimeToLocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static void main(String[] args) throws ParseException {
        String date = toFormatDate("19801027");
        System.out.println(date);
        System.out.println(parseDate(date));

        long l1 = convertTimeToTimestamp("2024-02-01 09:17:47");
        long l2 = convertTimeToTimestamp("2024-02-01 09:32:40");
        long l3 = convertTimeToTimestamp("2024-02-01 09:33:36");
        long l4 = convertTimeToTimestamp("2024-02-01 09:36:17");
        long l5 = convertTimeToTimestamp("2024-02-01 09:36:20");
        long l6 = convertTimeToTimestamp("2024-02-01 10:27:51");

        long secondsDifference = getSecondsDifference(l1, l2);
        long secondsDifference1 = getSecondsDifference(l3, l4);
        long secondsDifference2 = getSecondsDifference(l5, l6);
        long result = secondsDifference + secondsDifference1 + secondsDifference2;
        System.out.println(result);

        String time = TimeUtils.convertMillisecondsToTime(result * 1000);
        System.out.println("--" + time);

        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        double results = getDoubleRatio(0, 0L, decimalFormat);
        System.out.println("results==" + results);
    }

    /**
     * double占比-分子含有计算
     *
     * @param beforeNum
     * @param afterNum
     * @param decimalFormat
     * @return
     */
    public static double getDoubleRatio(double beforeNum, double afterNum, DecimalFormat decimalFormat) {
        System.out.println("beforeNum===" + beforeNum);
        System.out.println("afterNum===" + afterNum);
        return 0.0 == afterNum ? 0.0 :
                Double.parseDouble(decimalFormat.format((beforeNum - afterNum) / afterNum * 100.00));
    }

}
